﻿
// 2023/3/1: 首个版本
// 2023/3/1: 调整尺寸
// 2023/4/21: 修正机器码英文
// 2023/9/22: 使用PassParent给ShowDialog传参
// 2023/9/25: 改为直接输入Icon对象

using System;
using System.Xml;
using ASEva;
using ASEva.Samples;
using ASEva.UIEto;
using Eto.Forms;
using Eto.Drawing;

namespace CommonSource
{
    class LicenseChooser : DialogPanel
    {
        public LicenseChooser(String description, String rawMachineCode, bool chinese = false, String title = null, Icon icon = null)
        {
            Icon = icon;
            Title = !String.IsNullOrEmpty(title) ? title : (chinese ? "许可证文件选择" : "Choose license");
            SetFixMode(540, 135, true);

            String machineCode = null;
            if (rawMachineCode != null)
            {
                var cleanMachineCode = rawMachineCode.Replace('0', 'K').Replace('1', 'M').Replace('8', 'H').Replace('B', 'J').Replace('D', 'P'); // KM234567H9AJCPEF
                machineCode = cleanMachineCode.Substring(0, 2) + "-" +
                    cleanMachineCode.Substring(2, 2) + "-" +
                    cleanMachineCode.Substring(4, 2) + "-" +
                    cleanMachineCode.Substring(6, 2) + "-" +
                    cleanMachineCode.Substring(8, 2) + "-" +
                    cleanMachineCode.Substring(10, 2);
            }

            var mainLayout = this.SetContentAsColumnLayout();
            mainLayout.AddLabel(description, false);
            if (machineCode != null) mainLayout.AddLabel((chinese ? "机器码为：" : "Machine code is: ") + machineCode, false);
            var textBase64 = mainLayout.AddControl(new TextBox(), true) as TextBox;
            var buttonsLayout = mainLayout.AddRowLayout(true);

            var buttonQuit = buttonsLayout.AddButton(chinese ? "退出" : "Quit", false, 100);
            if (machineCode != null) buttonsLayout.AddButton(chinese ? "拷贝机器码至剪贴板" : "Copy code to clipboard", false, 170).Click += delegate { Clipboard.Instance.Text = machineCode; };
            buttonsLayout.AddSpace();
            var buttonChoose = buttonsLayout.AddButton(chinese ? "选择文件..." : "From file...", false, 100);
            var buttonConfirm = buttonsLayout.AddButton(chinese ? "确认" : "Confirm", false, 100);
            buttonConfirm.Enabled = false;

            buttonConfirm.Click += delegate
            {
                BoolResult = true;
                Close();
            };

            buttonQuit.Click += delegate
            {
                Close();
            };

            buttonChoose.Click += delegate
            {
                var dialog = new OpenFileDialog();
                dialog.Title = chinese ? "许可证文件选择" : "Load from a license file";
                dialog.Filters.Add(new FileFilter(chinese ? "许可证 (*.aslic)" : "License (*.aslic)", ".aslic"));

                if (dialog.ShowDialog(App.PassParent(this)) != DialogResult.Ok) return;

                var doc = new XmlDocument();
                try
                {
                    doc.Load(dialog.FileName);

                    String licenseStr = doc.DocumentElement.Attributes["license"].Value;
                    Convert.FromBase64String(licenseStr);
                    LicenseCode = licenseStr;

                    PluginCode = doc.DocumentElement.Attributes["plugin_code"].Value;
                    
                    textBase64.Text = licenseStr;
                }
                catch (Exception)
                {
                    MessageBox.Show(chinese ? "该许可证文件已损坏。" : "The license file is corrupted.", MessageBoxType.Error);
                    return;
                }
            };

            textBase64.TextChanged += delegate
            {
                bool textOK = true;
                if (textBase64.Text.Length == 0) textOK = false;
                try { Convert.FromBase64String(textBase64.Text); }
                catch (Exception) { textOK = false; }
                buttonConfirm.Enabled = textOK;
                textBase64.TextColor = textOK ? Colors.Green : Colors.Red;
            };
        }

        public String LicenseCode { get; set; }

        public String PluginCode { get; set; }
    }
}
